package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.api.CartsForOrder;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.remote.ProductApiClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ClearCartItemHandler extends AbstractTransHandler {

    @Autowired
    ProductApiClient productApiClient;


    //是否采用异步方式执行
    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext orderContext = (CreateOrderContext) context;
        CartsForOrder cartsForOrder = new CartsForOrder();
        cartsForOrder.setUserId(orderContext.getUserId().toString());
        cartsForOrder.setDeleteGoodsProductIds(orderContext.getBuyProductIds());
        Integer code = productApiClient.deleteCarts(cartsForOrder);
        if (code == 0) {
            throw new BizException(OrderRetCode.CALL_CART_PRODUCT_BY_USERID_CLEAR_FAIL.getCode(),OrderRetCode.CALL_CART_PRODUCT_BY_USERID_CLEAR_FAIL.getMessage());
        }
        return true;
    }
}
